package com.symaster.common.geom.show.node.element.menu.top;

import com.symaster.common.geom.show.node.api.TopMenu;
import com.symaster.common.geom.show.node.config.IconLoader;

import javax.swing.*;
import java.awt.*;

/**
 * @author yinmiao
 * @version 2023/7/7
 */
public class SelectTools extends TopMenu {

    private JPanel root_pane;
    private JButton check_select_button;
    private JButton check_move_button;
    private JButton check_zoom_button;
    private JButton check_rotate_button;
    private TypeEnum type;

    public SelectTools() {
        this.check_select_button.setIcon(IconLoader.select_b);
        this.check_move_button.setIcon(IconLoader.move_b);
        this.check_zoom_button.setIcon(IconLoader.zoom_b);
        this.check_rotate_button.setIcon(IconLoader.crop_rotate_b);

        initButtonListener();
    }

    private void initButtonListener() {
        type = TypeEnum.move;
        this.check_move_button.setIcon(IconLoader.move_a);

        check_select_button.addActionListener(e -> {
            if (type == TypeEnum.select) {
                return;
            }
            allToB();
            this.check_select_button.setIcon(IconLoader.select_a);
            type = TypeEnum.select;
        });
        check_move_button.addActionListener(e -> {
            if (type == TypeEnum.move) {
                return;
            }
            allToB();
            this.check_move_button.setIcon(IconLoader.move_a);
            type = TypeEnum.move;
        });
        check_zoom_button.addActionListener(e -> {
            if (type == TypeEnum.zoom) {
                return;
            }
            allToB();
            this.check_zoom_button.setIcon(IconLoader.zoom_a);
            type = TypeEnum.zoom;
        });
        check_rotate_button.addActionListener(e -> {
            if (type == TypeEnum.rotate) {
                return;
            }
            allToB();
            this.check_rotate_button.setIcon(IconLoader.crop_rotate_a);
            type = TypeEnum.rotate;
        });
    }

    private void allToB() {
        if (type == TypeEnum.select) {
            this.check_select_button.setIcon(IconLoader.select_b);
        } else if (type == TypeEnum.move) {
            this.check_move_button.setIcon(IconLoader.move_b);
        } else if (type == TypeEnum.zoom) {
            this.check_zoom_button.setIcon(IconLoader.zoom_b);
        } else if (type == TypeEnum.rotate) {
            this.check_rotate_button.setIcon(IconLoader.crop_rotate_b);
        }
    }

    @Override
    public String title() {
        return "选择工具栏";
    }

    @Override
    public JComponent body() {
        return root_pane;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root_pane = new JPanel();
        root_pane.setLayout(new BorderLayout(0, 0));
        final JToolBar toolBar1 = new JToolBar();
        root_pane.add(toolBar1, BorderLayout.CENTER);
        check_select_button = new JButton();
        check_select_button.setText("");
        toolBar1.add(check_select_button);
        check_move_button = new JButton();
        check_move_button.setText("");
        toolBar1.add(check_move_button);
        check_zoom_button = new JButton();
        check_zoom_button.setText("");
        toolBar1.add(check_zoom_button);
        check_rotate_button = new JButton();
        check_rotate_button.setText("");
        toolBar1.add(check_rotate_button);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root_pane;
    }

    private enum TypeEnum {
        select,
        move,
        zoom,
        rotate;
    }
}
